package com.magicsoft.share.adapter.exchange;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;

import com.magicsoft.share.bean.exchange.GoodsType;
import com.magicsoft.share.ui.exchange.GoodsFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lyf on 2017/10/25/025.
 */

public class GoodsFragmentAdapter extends FragmentPagerAdapter {

    private  GoodsFragment goodsFragment1;
    private  GoodsFragment goodsFragment2;
    private  GoodsFragment goodsFragment3;
    private  GoodsFragment goodsFragment4;
    private  GoodsFragment goodsFragment5;
    private  GoodsFragment goodsFragment6;
    private  GoodsFragment goodsFragment7;
    private  GoodsFragment goodsFragment8;
    private List<GoodsFragment> mFragments = new ArrayList<>();
    private List<GoodsType> mTypeList = new ArrayList<>();

    public GoodsFragmentAdapter(FragmentManager fm) {
        super(fm);

        goodsFragment1 = new GoodsFragment();
        goodsFragment2 = new GoodsFragment();
        goodsFragment3 = new GoodsFragment();
        goodsFragment4 = new GoodsFragment();
        goodsFragment5 = new GoodsFragment();
        goodsFragment6 = new GoodsFragment();
        goodsFragment7 = new GoodsFragment();
        goodsFragment8 = new GoodsFragment();

        mFragments.add(goodsFragment1);
        mFragments.add(goodsFragment2);
        mFragments.add(goodsFragment3);
        mFragments.add(goodsFragment4);
        mFragments.add(goodsFragment5);
        mFragments.add(goodsFragment6);
        mFragments.add(goodsFragment7);
        mFragments.add(goodsFragment8);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public int getCount() {
        return mTypeList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTypeList.get(position).getType_name();
    }


    public void refreshData( List<GoodsType> articleTypeList,int selectPosition) {

        mTypeList = articleTypeList;
        for (int i = 0; i < mFragments.size(); i++) {
            GoodsFragment fragment = mFragments.get(i);
            GoodsType goodsType = mTypeList.get(i);
            fragment.setType(goodsType.getId());
            if (selectPosition==i){
                Log.d("luf", "selectPosition: "+selectPosition+","+i);
                fragment.refreshData();
            }

        }
        this.notifyDataSetChanged();
    }

    public void setData( List<GoodsType> articleTypeList) {

        mTypeList = articleTypeList;
        for (int i = 0; i < mFragments.size(); i++) {
            GoodsFragment fragment = mFragments.get(i);
            GoodsType goodsType = mTypeList.get(i);
            fragment.setType(goodsType.getId());


        }
        this.notifyDataSetChanged();
    }
}
